package com.app.newcsc.fragment.main;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.app.newcsc.BR;
import com.app.newcsc.R;
import com.app.newcsc.app.AppViewModelFactory;
import com.app.newcsc.databinding.FragmentTabTransactionBinding;
import com.app.newcsc.entity.Message;
import com.app.newcsc.entity.OtcConfig;
import com.app.newcsc.fragment.BaseFragmentPagerAdapter;
import com.app.newcsc.activity.order.OrderRecordFragment;
import com.app.newcsc.utils.SpUtils;
import com.app.newcsc.utils.TextUtil;
import com.app.newcsc.utils.Utils;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.bus.RxBus;

/**
 * 交易
 * @author: woking
 * @date: 2021/6/7
 * @description：
 */
public class TransactionFragment extends BaseFragment<FragmentTabTransactionBinding, TransactionViewModel> {

    private List<Fragment> mFragments;
    private List<String> titlePager;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_tab_transaction;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public TransactionViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(TransactionViewModel.class);
    }

    @Override
    public void initData() {
//        viewModel.requestNetWork();
//        viewModel.planWallet();
        viewModel.otcConfig();

        viewModel.mOtcConfig.observe(this, new Observer<OtcConfig>() {
            @Override
            public void onChanged(OtcConfig otcConfig) {
                Log.e("TAG","==========otcConfig=========onNext==2222==");
                if(otcConfig != null)
                {
                    mFragments = pagerFragment(otcConfig);
                    titlePager = pagerTitleString(otcConfig);
                    //设置Adapter
                    BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), mFragments, titlePager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
                    binding.viewPager.setAdapter(pagerAdapter);
                    binding.viewPager.setOffscreenPageLimit(3);
                    binding.tabs.setupWithViewPager(binding.viewPager);
                    binding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.tabs));
                }
                else
                {
                    List<Fragment> list = new ArrayList<>();
                    list.add(TransactionTabFragment.newInstance("",openTime));
                    list.add(TransactionTabFragment.newInstance("1-10",openTime));
                    list.add(TransactionTabFragment.newInstance("11-99",openTime));
                    list.add(TransactionTabFragment.newInstance("100-500",openTime));
                    mFragments = list;

                    List<String> listTitle = new ArrayList<>();
                    listTitle.add("全部");
                    listTitle.add("1-10");
                    listTitle.add("11-99");
                    listTitle.add("100-500");
                    titlePager = listTitle;
                    //设置Adapter
                    BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), mFragments, titlePager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
                    binding.viewPager.setAdapter(pagerAdapter);
                    binding.viewPager.setOffscreenPageLimit(3);
                    binding.tabs.setupWithViewPager(binding.viewPager);
                    binding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.tabs));
                }
            }
        });

//        mFragments = pagerFragment();
//        titlePager = pagerTitleString();
//        //设置Adapter
//        BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), mFragments, titlePager);
//        binding.viewPager.setAdapter(pagerAdapter);
//        binding.viewPager.setOffscreenPageLimit(10);
//        binding.tabs.setupWithViewPager(binding.viewPager);
//        binding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.tabs));
        binding.tvOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!Utils.isLogin())
                {
                    return;
                }
                SpUtils.put(getActivity(),"isLogin","1");
                startContainerActivity(OrderRecordFragment.class.getCanonicalName());
            }
        });

        binding.tvBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(binding.tvBuy.getTag().equals("false"))
                {
                    binding.tvBuy.setTextColor(getResources().getColor(R.color.my_color_000000));
                    binding.tvBuy.setTextSize(23);
                    binding.tvBuy.setTag("true");
                    int selectIndex = 0;
                    String selevtData = "";
                    if(binding.viewPager != null)
                    {
//                        binding.tabs.
                        selectIndex = binding.viewPager.getCurrentItem();
//                        Log.e("TAG",selectIndex + "=====selectIndex=====111=====message====HTTP==========");
                        if(titlePager != null && titlePager.size() > 0 && selectIndex < titlePager.size())
                        {
                            selevtData = titlePager.get(selectIndex);
                            if(!TextUtil.isEmpty(selevtData) && selevtData.equals("全部"))
                            {
                                selevtData = "";
                            }
//                            Log.e("TAG",selevtData + "=====selevtData=====222=====message====HTTP==========");
                        }
                    }
                    RxBus.getDefault().post(new Message("buy",selevtData));

                    binding.tvSell.setTextColor(getResources().getColor(R.color.my_color_939393));
                    binding.tvSell.setTextSize(15);
                }
            }
        });
        binding.tvSell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(binding.tvBuy.getTag().equals("true"))
                {
                    binding.tvBuy.setTextColor(getResources().getColor(R.color.my_color_939393));
                    binding.tvBuy.setTextSize(15);
                    binding.tvBuy.setTag("false");
                    int selectIndex = 0;
                    String selevtData = "";
                    if(binding.viewPager != null)
                    {
                        selectIndex = binding.viewPager.getCurrentItem();
//                        Log.e("TAG",selectIndex + "=====selectIndex=====111=====message====HTTP==========");
                        if(titlePager != null && titlePager.size() > 0 && selectIndex < titlePager.size())
                        {
                            selevtData = titlePager.get(selectIndex);
                            if(!TextUtil.isEmpty(selevtData) && selevtData.equals("全部"))
                            {
                                selevtData = "";
                            }
//                            Log.e("TAG",selevtData + "=====selevtData=====222=====message====HTTP==========");
                        }
                    }
                    RxBus.getDefault().post(new Message("sell",selevtData));

                    binding.tvSell.setTextColor(getResources().getColor(R.color.my_color_000000));
                    binding.tvSell.setTextSize(23);
                }
            }
        });
    }

    @Override
    public void initViewObservable() {

    }

    public static String openTime = "开市时间:09:00-21:00";
    protected List<Fragment> pagerFragment(OtcConfig otcConfig) {
        List<Fragment> list = new ArrayList<>();
        String time = "开市时间：" + otcConfig.open_time + "-" + otcConfig.close_time;
        openTime = time;
        list.add(TransactionTabFragment.newInstance("",time));
        if(otcConfig != null && otcConfig.num_zone != null && otcConfig.num_zone.size() > 0)
        {
            List<Map<String,Object>> num_zone = otcConfig.num_zone;
            for(int i = 0;i < num_zone.size(); i++)
            {
                Map<String,Object> map = num_zone.get(i);
                for (Object value : map.values()) {
                    list.add(TransactionTabFragment.newInstance((String)value,time));
                }
            }
        }
//        list.add(TransactionTabFragment.newInstance("1-10"));
//        list.add(TransactionTabFragment.newInstance("11-99"));
//        list.add(TransactionTabFragment.newInstance("100-500"));
        return list;
    }

    protected List<String> pagerTitleString(OtcConfig otcConfig) {
        List<String> list = new ArrayList<>();
        list.add("全部");
        if(otcConfig != null && otcConfig.num_zone != null && otcConfig.num_zone.size() > 0)
        {
            List<Map<String,Object>> num_zone = otcConfig.num_zone;
            for(int i = 0;i < num_zone.size(); i++)
            {
                Map<String,Object> map = num_zone.get(i);
                for (Object value : map.values()) {
                    list.add((String)value);
                }
            }
        }
//        list.add("1-10");
//        list.add("11-99");
//        list.add("100-500");
        return list;
    }
}